package com.ocom.devicehttp.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.payconfig.EntityDishesClass;
import com.ocom.common.entity.payconfig.EntityMqttDevice;
import com.ocom.common.request.payconfig.*;
import com.ocom.common.request.payconfig.adreceiptinfo.GetAdReceiptInfoRequest;
import com.ocom.common.request.payconfig.mealorder.*;
import com.ocom.common.request.payhandle.GetPointMealRequest;
import com.ocom.common.request.payhandle.GetWxGhParamRequest;
import com.ocom.common.request.payhandle.OrderVerificationRequest;
import com.ocom.common.request.web.ConsTimeConfigOnShowRequest;
import com.ocom.common.request.xfrecde.GetUserOrdered;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.JsonResponse;
import com.ocom.common.utils.StateCode;
import com.ocom.common.vo.devicehttp.DishesClassListVo;
import com.ocom.common.vo.devicehttp.OrderMealStatusListVo;
import com.ocom.common.vo.emqx.GetDeviceArgsVo;
import com.ocom.common.vo.payconfig.OrderPlanMealVo;
import com.ocom.common.vo.pyhandle.GetPointMealVo;
import com.ocom.common.vo.xfrecode.OrderedMealVo;
import com.ocom.devicehttp.feign.machine.PayConfigClientService;
import com.ocom.devicehttp.feign.machine.PayoperFeignService;
import com.ocom.devicehttp.feign.machine.XfRecodeClientService;
import com.ocom.devicehttp.service.DishesService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


@Slf4j
@Service
public class DishesServiceImpl implements DishesService {

    @Autowired
    PayoperFeignService payoperFeignService;

    @Autowired
    PayConfigClientService payConfigClientService;

    @Autowired
    XfRecodeClientService xfRecodeClientService;

    @Resource
    private RedisUtil redisUtil;

    @Override
    public JsonResponse orderMenuType(Map<String, Object> map) {
        log.info("点餐菜系更新 orderMenuType 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());


            String mealDate = null;
            Integer mealNo = null;
            try {
                mealDate = (String) map.get("mealDate");
                mealNo = (Integer) map.get("mealNo");
            } catch (Exception exception) {
                log.error("orderMenuType 获取参数异常【{}】", exception);
            }


            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                GetPointMealRequest getPointMealRequest = new GetPointMealRequest();
                getPointMealRequest.setCompanyId(companyID);
                getPointMealRequest.setMachNo(machno);

                Result result = payoperFeignService.getPointMeal(getPointMealRequest);

                if (result.getCode() != 200) {
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }
                GetPointMealVo getPointMealVo = JSON.parseObject(JSON.toJSONString(result.getData()), GetPointMealVo.class);

                GetOrderPlanDishesRequest getOrderPlanDishesRequest = new GetOrderPlanDishesRequest();
                getOrderPlanDishesRequest.setComId(companyID);
//                getOrderPlanDishesRequest.setMerchId(getPointMealVo.);
                getOrderPlanDishesRequest.setPointId(getPointMealVo.getPointId());

                if (mealDate != null) {
                    getOrderPlanDishesRequest.setMealDate(mealDate);
                } else {
                    getOrderPlanDishesRequest.setMealDate(getPointMealVo.getMealDate());
                }
                if (mealNo != null) {
                    getOrderPlanDishesRequest.setMealNo(mealNo);
                } else {
                    getOrderPlanDishesRequest.setMealNo(getPointMealVo.getMealNo());
                }
                if (getOrderPlanDishesRequest.getMealNo() == 0) {
                    getOrderPlanDishesRequest.setMealNo(2);
                }
//                getOrderPlanDishesRequest.setReceiptId(0);

                Result orderPlanDishes = payConfigClientService.getOrderPlanDishes(getOrderPlanDishesRequest);
                if (orderPlanDishes.getCode() != 200 || orderPlanDishes.getData() == null) {
                    return JsonResponse.error(orderPlanDishes.getCode().toString(), "无数据-排餐计划");
                }

                List<GetOrderPlanDishesResponse> responseList = JSON.parseArray(JSON.toJSONString(orderPlanDishes.getData()), GetOrderPlanDishesResponse.class);

                List<Integer> classIds = responseList.stream().map(GetOrderPlanDishesResponse::getDishesClassId).collect(Collectors.toList());

                Result dishesClassOfCom = payConfigClientService.dishesClassOfCom(companyID);
                if (dishesClassOfCom.getCode() != 200 || dishesClassOfCom.getData() == null) {
                    return JsonResponse.error(orderPlanDishes.getCode().toString(), "无数据-菜品分类");
                }

                List<EntityDishesClass> dishesClassList = JSON.parseArray(JSON.toJSONString(dishesClassOfCom.getData()), EntityDishesClass.class);


                List<DishesClassListVo> dishesClassListVos = new ArrayList<>();


                for (EntityDishesClass entityDishesClass : dishesClassList) {
                    if (classIds.contains(entityDishesClass.getDishesClassId())) {
                        DishesClassListVo dishesClassListVo = new DishesClassListVo();
                        dishesClassListVo.setDishesClassId(entityDishesClass.getDishesClassId());
                        dishesClassListVo.setDishesClassName(entityDishesClass.getDishesClassName());
                        dishesClassListVos.add(dishesClassListVo);
                    }
                }

                Map<String, Object> resMap = new HashMap<>();
                resMap.put("classList", dishesClassListVos);
                resMap.put("mealNo", getPointMealVo.getMealNo());

                return JsonResponse.success(resMap);
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse orderMenuList(Map<String, Object> map) {
        log.info("点餐菜系更新 orderMenuType 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");

            Object claOb = map.get("dishesClassId");

            Integer dishesClassId = 0;
            if (claOb != null) {
                dishesClassId = (Integer) claOb;
            }

            String mealDate = null;
            Integer mealNo = null;
            try {
                if (map.get("mealDate") != null) {
                    Map<String, Object> sear = JSON.parseObject(JSON.toJSONString(map.get("mealDate")), HashMap.class);
                    mealDate = (String) sear.get("mealDate");
                    mealNo = (Integer) sear.get("mealNo");
                }
            } catch (Exception exception) {
                log.error("orderMenuList 获取参数异常【{}】", exception);
            }

//            Integer page = (Integer) map.get("page");
//            Integer size = (Integer) map.get("num");

            /**
             * mealDate	餐段日期	string
             * mealNo	餐段编号	string
             * status	订餐状态		1.未订，截止订餐时间后还未订餐；
             * 2.已订，已订餐；
             * 3.可订，截止订餐时间前还未订餐；
             * 4.——，不可订，未发布订餐信息或未到可订餐时间。
             */

            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                GetPointMealRequest getPointMealRequest = new GetPointMealRequest();
                getPointMealRequest.setCompanyId(companyID);
                getPointMealRequest.setMachNo(machno);

                Result result = payoperFeignService.getPointMeal(getPointMealRequest);

                if (result.getCode() != 200) {
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }
                GetPointMealVo getPointMealVo = JSON.parseObject(JSON.toJSONString(result.getData()), GetPointMealVo.class);

                GetOrderPlanDishesRequest getOrderPlanDishesRequest = new GetOrderPlanDishesRequest();
                getOrderPlanDishesRequest.setComId(companyID);
//                getOrderPlanDishesRequest.setMerchId(getPointMealVo.);
                getOrderPlanDishesRequest.setPointId(getPointMealVo.getPointId());

                if (mealDate != null) {
                    getOrderPlanDishesRequest.setMealDate(mealDate);
                } else {
                    getOrderPlanDishesRequest.setMealDate(getPointMealVo.getMealDate());
                }
                if (mealNo != null) {
                    getOrderPlanDishesRequest.setMealNo(mealNo);
                } else {
                    getOrderPlanDishesRequest.setMealNo(getPointMealVo.getMealNo());
                }

                if (getOrderPlanDishesRequest.getMealNo() == 0) {
                    getOrderPlanDishesRequest.setMealNo(2);
                }

                getOrderPlanDishesRequest.setReceiptId(0);

                Result orderPlanDishes = payConfigClientService.getOrderPlanDishes(getOrderPlanDishesRequest);
                if (orderPlanDishes.getCode() != 200 || orderPlanDishes.getData() == null) {
                    return JsonResponse.error(orderPlanDishes.getCode().toString(), "无数据-排餐计划");
                }

                List<GetOrderPlanDishesResponse> responseList = JSON.parseArray(JSON.toJSONString(orderPlanDishes.getData()), GetOrderPlanDishesResponse.class);
                Map<String, Object> resMap = new HashMap<>();
                resMap.put("dishList", responseList);
                if (dishesClassId == 0) {
                    return JsonResponse.success(resMap);
                }
                List<GetOrderPlanDishesResponse> responseList1 =  new ArrayList<>();
                for (GetOrderPlanDishesResponse getOrderPlanDishesResponse:responseList) {
                    if (getOrderPlanDishesResponse.getDishesClassId().equals(dishesClassId)) {
                        responseList1.add(getOrderPlanDishesResponse);
                    }
                }
                resMap.put("dishList", responseList1);
                return JsonResponse.success(resMap);

            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse menuListQuery(Map<String, Object> map) {
        log.info("点餐菜系更新 orderMenuType 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");

            String mealDate = (String) map.get("mealDate");
            Integer days = (Integer) map.get("days");
            Object pIdOb = map.get("pId");
            Integer pId = 0;
            if (pIdOb != null) {
                pId = (Integer) map.get("pId");
            }

            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                Result mealNameInfo = payConfigClientService.GetMealNameInfo(companyID);

                Map<String, Object> mealNameInfoData = JSON.parseObject(JSON.toJSONString(mealNameInfo.getData()), HashMap.class);
                List<EntityXfMealInfo> entityXfMealInfos = JSON.parseArray(JSON.toJSONString(mealNameInfoData.get("list")), EntityXfMealInfo.class);

                List<String> dayList = new ArrayList<>();

                dayList.add(mealDate);

                if(days>1){
                    for (int i = 1; i < days; i++) {
                        Date date = DateUtil.addDays(DateUtils.strToDate2(mealDate), i);
                        dayList.add(DateUtils.getStrDate(date, DateUtils.YYYY_MM_DD));
                    }
                }


                List<OrderMealStatusListVo> list = new ArrayList<>();


                //文档第一步和第二步 合在一起
                for (int i = 0; i < dayList.size(); i++) {
                    for (EntityXfMealInfo entityXfMealInfo : entityXfMealInfos) {
                        OrderMealStatusListVo orderMealStatusListVo = new OrderMealStatusListVo();
                        orderMealStatusListVo.setMealDate(dayList.get(i));
                        orderMealStatusListVo.setMealNo(entityXfMealInfo.getMealNo());
                        orderMealStatusListVo.setShow("未订");
                        orderMealStatusListVo.setStatus(0);
                        list.add(orderMealStatusListVo);
                    }
                }

                for (int i = 0; i < dayList.size(); i++) {
                    List<OrderPlanMealVo> orderPlanMealVos = new ArrayList<>();
                    GetOrderPlanMealRequest getOrderPlanMealRequest = new GetOrderPlanMealRequest();
                    getOrderPlanMealRequest.setMealDate(dayList.get(i));
                    getOrderPlanMealRequest.setComId(companyID);
                    Result result2 = payConfigClientService.getOrderPlanMeal(getOrderPlanMealRequest);
                    if (result2.getCode() == 200 && result2.getData() != null) {
                        orderPlanMealVos = JSON.parseArray(JSON.toJSONString(result2.getData()), OrderPlanMealVo.class);
                    } else {
                        continue;
                    }

                    for (int j = 0; j < list.size(); j++) {
                        OrderMealStatusListVo orderMealStatusListVo = list.get(j);
                        for (OrderPlanMealVo orderPlanMealVo : orderPlanMealVos) {
                            if (orderPlanMealVo.getMealDate().equals(orderMealStatusListVo.getMealDate())
                                    && orderPlanMealVo.getMealNo().equals(orderMealStatusListVo.getMealNo())
                            ) {
                                orderMealStatusListVo.setStatus(3);
                                orderMealStatusListVo.setShow("可订");
                                orderMealStatusListVo.setInfoIds(orderPlanMealVo.getInfoIds() == null ? "" : orderPlanMealVo.getInfoIds());
                                list.set(j, orderMealStatusListVo);
                            }
                        }
                    }
                }

                if (pId == 0) {
                    Map<String, Object> resMap = new HashMap<>();
                    resMap.put("mealData", list);
                    return JsonResponse.success(resMap);
                }

                GetUserOrdered getUserOrdered = new GetUserOrdered();
                getUserOrdered.setComId(companyID);
                getUserOrdered.setPId(pId.longValue());
                getUserOrdered.setBeginDate(dayList.get(0));
                getUserOrdered.setEndDate(dayList.get(dayList.size() - 1));
                Result result = xfRecodeClientService.getUserOrdered(getUserOrdered);

                if (result.getCode() == 200 && result.getData() != null) {
                    List<OrderedMealVo> mealVos = JSON.parseArray(JSON.toJSONString(result.getData()), OrderedMealVo.class);


                    for (int j = 0; j < list.size(); j++) {
                        OrderMealStatusListVo orderMealStatusListVo = list.get(j);
                        for (OrderedMealVo orderedMealVo : mealVos) {
                            if (orderedMealVo.getMealDate().equals(orderMealStatusListVo.getMealDate())
                                    && orderedMealVo.getMealNo().equals(Integer.valueOf(orderMealStatusListVo.getMealNo()))
                            ) {
                                orderMealStatusListVo.setStatus(2);
                                orderMealStatusListVo.setShow("已订");
                                list.set(j, orderMealStatusListVo);
                            }
                        }
                    }
                }

                Map<String, Object> resMap = new HashMap<>();
                resMap.put("mealData", list);
                return JsonResponse.success(resMap);
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse informationQuery(Map<String, Object> map) {
        log.info("点餐菜系更新 orderMenuType 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                GetPointMealRequest getPointMealRequest = new GetPointMealRequest();
                getPointMealRequest.setCompanyId(companyID);
                getPointMealRequest.setMachNo(machno);

                Result result = payoperFeignService.getPointMeal(getPointMealRequest);

                if (result.getCode() != 200) {
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }
                GetPointMealVo getPointMealVo = JSON.parseObject(JSON.toJSONString(result.getData()), GetPointMealVo.class);

                GetPointDishesRequest request = new GetPointDishesRequest();
                request.setComId(companyID);
                request.setMealDate(getPointMealVo.getMealDate());
                request.setPointId(getPointMealVo.getPointId());
                request.setMealNo(getPointMealVo.getMealNo());
                Result pointDishes = payConfigClientService.getPointDishes(request);
                if (result.getCode() != 200) {
                    return JsonResponse.error(pointDishes.getCode().toString(), pointDishes.getMsg());
                }


                return JsonResponse.success(pointDishes);
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse personQuery(Map<String, Object> map) {
        log.info("点餐菜系更新 orderMenuType 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());
//            Long pId = Long.valueOf(map.get("pId").toString());
//            String mealDate = (String) map.get("mealDate");
//            Integer mealNo = (Integer) map.get("mealNo");

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                Map<String, Object> resMap = new HashMap<>();

                Map<String, Object> quMap = new HashMap<>();
                quMap.put("companyID", companyID);
                quMap.put("type", (Integer) map.get("type") );
                quMap.put("cardNum", map.get("authCode").toString());
                RespMachMessage respMachMessage = payoperFeignService.getBalance(quMap);

                if(!respMachMessage.getCode().equals("0000")){
                    return JsonResponse.error(respMachMessage.getCode(), respMachMessage.getMsg());
                }

                resMap.put("accountInf",respMachMessage.getData());

//                GetOrderListByMealRequest request =new GetOrderListByMealRequest();
//                request.setCompanyId(companyID);
//                request.setPId(pId);
//                request.setMealDate(mealDate);
//                request.setMealNo(mealNo);
//
//                Result result = payoperFeignService.getOrderListByMeal(request);
//                if(result.getCode()!=200){
//                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
//                }
//                resMap.put("orderList",result.getData());

                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(respMachMessage.getData()));

                String beginDate = (String) map.get("beginDate");
                String endDate = (String) map.get("endDate");

                GetOrderDishesRequest getOrderDishesRequest =new GetOrderDishesRequest();
                getOrderDishesRequest.setComId(companyID);
                getOrderDishesRequest.setPId(jsonObject.getLong("pId"));
                getOrderDishesRequest.setBeginDate(beginDate);
                getOrderDishesRequest.setEndDate(endDate);
                getOrderDishesRequest.setPage(1);
                getOrderDishesRequest.setSize(500);
                Result result  = payConfigClientService.getOrderDishes(getOrderDishesRequest);

                resMap.put("orderList",result.getData());

                ConsTimeConfigOnShowRequest request3 =new ConsTimeConfigOnShowRequest();
                request3.setComId(companyID);
                request3.setPage(1);
                request3.setSize(10);
                Result result2=payConfigClientService.GetMealTimeInfo(request3);

                resMap.put("orderTime",result2.getData());

                return JsonResponse.success(resMap);
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse orderVerification(Map<String, Object> map) {
        log.info("核销 orderVerification 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());
            String authCode = (String) map.get("authCode");
            Integer type = (Integer) map.get("type");

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                Map<String, Object> resMap = new HashMap<>();

                OrderVerificationRequest orderVerificationRequest=new OrderVerificationRequest();
                orderVerificationRequest.setCompanyId(companyID);
                orderVerificationRequest.setType(type);
                orderVerificationRequest.setAuthCode(authCode);
                orderVerificationRequest.setMachNo(machno);
                Result result = payoperFeignService.orderVerification(orderVerificationRequest);


                if(result.getCode()!=200){
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }

                return JsonResponse.success(result.getData());
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse adReceiptQuery(Map<String, Object> map) {
        log.info("获取取餐点列表 adReceiptQuery 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                Map<String, Object> resMap = new HashMap<>();

//                OrderVerificationRequest orderVerificationRequest=new OrderVerificationRequest();
//                orderVerificationRequest.setCompanyId(companyID);
//                orderVerificationRequest.setType(type);
//                orderVerificationRequest.setAuthCode(authCode);
//                orderVerificationRequest.setMachNo(machno);
//                Result result = payoperFeignService.orderVerification(orderVerificationRequest);

                GetAdReceiptInfoRequest getAdReceiptInfoRequest=new GetAdReceiptInfoRequest();
                getAdReceiptInfoRequest.setComId(companyID);
                getAdReceiptInfoRequest.setPage(1);
                getAdReceiptInfoRequest.setSize(500);
                Result result = payConfigClientService.getAdReceiptInfo(getAdReceiptInfoRequest);

                if(result.getCode()!=200){
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }

                return JsonResponse.success(result.getData());
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse orderPointQuery(Map<String, Object> map) {
        log.info("查询该餐段可订消费点 orderPointQuery 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            Integer receiptId = (Integer) map.get("receiptId");
            String mealDate = (String) map.get("mealDate");
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                Map<String, Object> resMap = new HashMap<>();
                GetMerchPointListRequest request=new GetMerchPointListRequest();
                request.setComId(companyID);
                request.setReceiptId(receiptId);
                request.setMealDate(mealDate);
                Result result = payConfigClientService.getMerchPointList(request);
                if(result.getCode()!=200){
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }
                return JsonResponse.success(result.getData());
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse phoneInitPara(Map<String, Object> map) {
        log.info("查询该餐段可订消费点 orderPointQuery 【{}】", map);
        String machno = "";
        try {
            //机器号
            machno = (String) map.get("machNo");
            Object typeObj = map.get("type");
            Integer type = 3;
            if(typeObj != null){
                type = (Integer) typeObj;
            }
            //商户编号
            Long companyID = Long.valueOf(map.get("companyID").toString());

            Integer saasNo = (Integer) redisUtil.hmGet("HttpXfDevice:"+machno, "saasNo");
            //平台编号为1按照本平台走
            if (saasNo == 1) {

                Map<String, Object> resMap = new HashMap<>();
                GetWxGhParamRequest request=new GetWxGhParamRequest();
                request.setCompanyId(companyID);
                request.setType(type);
                request.setDeviceNo(machno);
                Result result = payoperFeignService.getWxGhParam(request);
                if(result.getCode()!=200){
                    return JsonResponse.error(result.getCode().toString(), result.getMsg());
                }
                return JsonResponse.success(result.getData());
            } else {
                return JsonResponse.error(StateCode.saasNoCod, StateCode.saasNoMsg);
            }
        } catch (Exception e) {
            log.info("机器号:【{}】,微信公话-请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse ShareInitPara(Map<String, Object> map) {
        log.info("获取洗衣机/吹风机设备列表,请求参数{}",JSON.toJSONString(map));
        String machno = "";
        try {
            GetMqttDeviceRequest getMqttDeviceRequest = new GetMqttDeviceRequest();
            machno = (String) map.get("machNo");
            Long companyID = Long.valueOf(map.get("companyID").toString()) ;
            getMqttDeviceRequest.setComId(companyID);
            getMqttDeviceRequest.setMachNo(machno);
            Result<List<GetDeviceArgsVo>> resultA = this.payConfigClientService.getMqttDevice(getMqttDeviceRequest);
//            if(resultA.getCode().equals(200)){
//                return JsonResponse.success(resultA.getData());
//            }
            return JsonResponse.success(resultA.getData());
        }catch (Exception e){
            log.info("机器号:【{}】,获取洗衣机/吹风机设备列表-请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }

    }

    @Override
    public JsonResponse newMqttOrder(Map<String, Object> map) {
        log.info("洗衣机/吹风机设备提交订单,请求参数{}",JSON.toJSONString(map));
        String machno = "";
        try {
            machno = (String) map.get("machNo");
            Long companyID = Long.valueOf(map.get("companyID").toString()) ;
            Integer price = Integer.valueOf(map.get("price").toString());
            Long pid = Long.valueOf(map.get("pId").toString());
            String tradeNo = map.get("tradeNo").toString();
            Long mqttDeviceId = Long.valueOf(map.get("mqttDeviceId").toString());
            String operateCode = map.get("operateCode").toString();
            //获取设备ID
            NewMqttOrderRequest newMqttOrderRequest = new NewMqttOrderRequest();
            newMqttOrderRequest.setComId(companyID);
            newMqttOrderRequest.setMachNo(machno);
            newMqttOrderRequest.setPrice(price);
            newMqttOrderRequest.setPId(pid);
            newMqttOrderRequest.setTradeNo(tradeNo);
            newMqttOrderRequest.setMqttDeviceId(mqttDeviceId);
            newMqttOrderRequest.setOperateCode(operateCode);
            Result<EntityMqttDevice> result = this.payConfigClientService.newMqttOrder(newMqttOrderRequest);
            if(result.getCode().equals(200)){
                Map<String,Object> rMap = new HashMap<>();
                rMap.put("orderId",result.getData().getOrderId());
                return JsonResponse.success(rMap);
            }
            log.info("机器号:【{}】,洗衣机/吹风机设备提交订单-请求参数:【{}】,返回参数:【{}】", machno, map, result);
            return JsonResponse.error(result.getMsg());
        }catch (Exception e){
            log.info("机器号:【{}】,洗衣机/吹风机设备提交订单-请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }

    }

    @Override
    public JsonResponse payMqttOrder(Map<String, Object> map) {
        log.info("获取洗衣机/吹风机设备工作-订单支付成功,请求参数{}",JSON.toJSONString(map));
        String machno = "";
        try {
            machno = (String) map.get("machNo");
            Long orderId = Long.valueOf(map.get("orderId").toString());
            Long mqttDeviceId = Long.valueOf(map.get("mqttDeviceId").toString());
            Long companyID = Long.valueOf(map.get("companyID").toString()) ;
            Long pid = Long.valueOf(map.get("pId").toString());
            String tradeNo = map.get("tradeNo").toString();
            PayMqttOrderRequest payMqttOrderRequest = new PayMqttOrderRequest();
            payMqttOrderRequest.setOrderId(orderId);
            payMqttOrderRequest.setMqttDeviceId(mqttDeviceId);
            payMqttOrderRequest.setCompanyId(companyID);
            payMqttOrderRequest.setMachNo(machno);
            payMqttOrderRequest.setPId(pid);
            payMqttOrderRequest.setTradeNo(tradeNo);
            Result result = this.payConfigClientService.payMqttOrder(payMqttOrderRequest);
            log.info("获取洗衣机/吹风机设备工作-启动状态,返回参数{}",JSON.toJSONString(result));
            return JsonResponse.success(result.getData());
        }catch (Exception e){
            log.error("异常机器号:【{}】,洗衣机/吹风机设备工作-订单支付成功:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }

    @Override
    public JsonResponse cancelMqttOrder(Map<String, Object> map) {
        log.info("洗衣机/吹风机设备工作-取消订单,请求参数{}",JSON.toJSONString(map));
        String machno = "";
        try {
            machno = (String) map.get("machNo");
            CancelMqttOrderRequest request = new CancelMqttOrderRequest();
            Long orderId = Long.valueOf(map.get("orderId").toString());
            Long mqttDeviceId = Long.valueOf(map.get("mqttDeviceId").toString());
            Long companyID = Long.valueOf(map.get("companyID").toString()) ;
            request.setOrderId(orderId);
            request.setMqttDeviceId(mqttDeviceId);
            request.setMachNo(machno);
            request.setComId(companyID);
            Map<String,Integer> resultMap = new HashMap<>();
            Result result  = this.payConfigClientService.cancelMqttOrder(request);
            if(result.getCode().equals(200)){
                resultMap.put("state",0);
              return JsonResponse.success(resultMap);
            }
            resultMap.put("state",1);
            return JsonResponse.error("0000",result.getMsg(),resultMap);
        }catch (Exception e){
            log.info("机器号:【{}】,洗衣机/吹风机设备工作-取消订单-请求参数:【{}】,返回参数:【{}】", machno, map, e);
            return JsonResponse.error();
        }
    }
}
